<?php

namespace app\index\validate;

use think\Validate;

class Sale extends Validate
{
    protected $rule =   [
        'title'   => 'require|chsAlphaNum|length:1,30',
        'starttime'  => 'require|dateFormat:Y-m-d',
        'endtime'   => 'require|dateFormat:Y-m-d',
        'paytype'   => 'require|in:0,1',
        'jump'   => 'require|require|in:card,real_share,url',
        'jump_url'   => 'requireIf:jump,url|url',
        'template'   => 'require|integer',
        'number'   => 'require|integer',
        'view'   => 'require|verJson',
        'real_share'   => 'require',
        'poi_name'   => 'require',
        'poi_address'   => 'require',
        'poi_share'   => 'require',
        'micro_app_id'   => 'require',
        'micro_app_url'   => 'require',
        'micro_app_title'   => 'require',
        'content'   => 'require',
        'replace'   => 'require|verJson',
        'video'   => 'require|verJson',
        'card'   => 'require|verJson',
        'scope'   => 'require',
        'id'   => 'require|integer',
        'poster'   => 'require|verJson',
    ];

    protected $field = [
        'title'=>'标题',
        'starttime'=>'开始时间',
        'endtime'   => '结束时间',
        'paytype'   => '活动类型',
        'jump'   => '跳转类型',
        'jump_url'   => '自定义跳转地址',
        'template'   => '活动模板',
        'number'   => '扫码次数',
        'real_share'   => '抖音商户ID',
        'poi_name'   => '抖音商户名称',
        'poi_address'   => '抖音商户联系地址',
        'micro_app_title'   => '小程序标题',
        'micro_app_id'   => '小程序APPid',
        'micro_app_url'   => '小程序url',
        'content'   => '活动内容',
        'replace'   => '页面配置',
        'video'   => '视频数据',
        'card'   => '卡片',
        'id'   => '活动ID',
        'poster'   => '推广图',
        'scope'   => '接口权限',
    ];

    protected $scene = [
        'add'=>[
            'title',
            'starttime',
            'endtime' ,
            'paytype' ,
            'jump' ,
            'jump_url' ,
            'template' ,
            'number',
            'real_share',
            'poi_name',
            'poi_address',
            'micro_app_title',
            'micro_app_id',
            'micro_app_url',
            'content',
            'replace',
            'video' ,
            'card',
            'poster',
            'scope',
],
        'edit'=>[
            'title',
            'starttime',
            'endtime' ,
            'paytype' ,
            'jump' ,
            'jump_url' ,
            'template' ,
            'number',
            'real_share',
            'poi_name',
            'poi_address',
            'micro_app_title',
            'micro_app_id',
            'micro_app_url',
            'content',
            'replace',
            'video' ,
            'card',
            'id',
            'poster',
            'scope',
            ]
    ];

    protected function verJson($value, $data){
        $video = json_encode($value);
        if(is_null($video)){
            return '数据格式不正确，请上传正确的json格式';
        }
        return true;
    }
}